﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    /*
        数据库的初始化，备份等操作

        初始化的目的是保证一些必要的数据添加到数据库中，甚至可以自动建立表（通过执行sql文件代码）等
    */
    public class ApiDataDictionary : ApiBase
    {
        public ApiDataDictionary(Controller ctx) : base(ctx) { }
        #region 自定义请求参数
        // 作者
        string req_author { get { return GetStringParam(new ReqStringParamOption { Name = "author", Level = REQPARAMLEVEL.FREE }); } }
        string req_dictionary_dictype { get { return GetStringParam(new ReqStringParamOption { Name = "dictype" }); } }
        int req_dictionary_parent_id { get { return GetIntParam(new ReqIntParamOption { Name = "parent_id" }); } }
        int req_is_visible { get { return GetIntParam(new ReqIntParamOption { Name = "is_visible" }); } }
        string req_typeName { get { return GetStringParam(new ReqStringParamOption { Name = "typeName", Level = REQPARAMLEVEL.FREE, DefaultValue = null }); } }
        public string req_logo_ids { get { return GetStringParam(new ReqStringParamOption { Name = "logo_ids" }); } }
        #endregion
        public void Do(string action)
        {
            switch (action)
            {
                case "dictionary_add":
                    {
                        DataDictionary datadictionary =new DataDictionary();
                        datadictionary.parent_id = req_dictionary_parent_id;
                        datadictionary.name = req_name;
                        datadictionary.dictype = req_dictionary_dictype;
                        datadictionary.logo_ids = req_logo_ids;
                        datadictionary.url = req_url;
                        datadictionary.remark = req_remark;
                        datadictionary.is_visible = 1;
                        datadictionary.create_time = DateTimeEx.GetNowTimeStamp();
                        DataDictionary.AddNode(datadictionary);
                        ResponseJson(new IDResult { id=datadictionary.id});
                        break;
                    }
                case "dictionary_update":
                    {
                        DataDictionary datadictionary = G.DB.GetById<DataDictionary>(req_id);
                        datadictionary.name = req_name;
                        datadictionary.logo_ids = req_logo_ids;
                        datadictionary.url = req_url;
                        datadictionary.remark = req_remark;
                        datadictionary.is_visible = 1;
                        datadictionary.create_time = DateTimeEx.GetNowTimeStamp();
                        DataDictionary.UpdataNode(datadictionary);
                        ResponseJson(new Result());
                        break;
                    }
                case "dictionary_tree":
                    {
                        if (req_typeName == null)
                        {
                            ResponseJson(new ObjectResult { data = DataDictionary.GetCache() });
                            break;
                        }
                        else
                        {
                            ResponseJson(new ObjectResult { data = DataDictionary.GetCache()[req_typeName] });
                            break;
                        }
                        
                    }
                case "dictionary_remove":
                    {
                        DataDictionary.RemoveNode(req_id);
                        ResponseJson(new Result());
                        break;
                    }
                default: break;
            }
        }

    }


}